package net.sinmetal.twitcockpit.modelvar;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.sinmetal.twitcockpit.model.constant.SchedulePostType;
import net.sinmetal.twitcockpit.util.DateUtil;
import net.sinmetal.twitcockpit.util.RandomUtil;
import net.sinmetal.twitcockpit.util.SecurityUtil;
import net.sinmetal.twitcockpit.util.StringUtil;

import org.slim3.util.BeanUtil;

import com.google.appengine.api.datastore.Key;

/**
 * AutoPostVar
 *
 * @author Sinmetal
 *
 */
public class AutoPostVar {

    private String id;

    private Key robotKey;

    private String twitterToken;

    private String twitterTokenSecret;

    private String tweet;

    private List<SchedulePostType> schedules;

    private int startYear;

    private int startMonth;

    private int startDay;

    private int startHour;

    private int startMinute;

    private int endYear;

    private int endMonth;

    private int endDay;

    private int endHour;

    private int endMinute;

    /**
     * @return id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id
     *            セットする id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return robotKey
     */
    public Key getRobotKey() {
        return robotKey;
    }

    /**
     * @param robotKey
     *            セットする robotKey
     */
    public void setRobotKey(Key robotKey) {
        this.robotKey = robotKey;
    }

    /**
     * @return twitterToken
     */
    public String getTwitterToken() {
        return twitterToken;
    }

    /**
     * @param twitterToken
     *            セットする twitterToken
     */
    public void setTwitterToken(String twitterToken) {
        this.twitterToken = twitterToken;
    }

    /**
     * @return twitterTokenSecret
     */
    public String getTwitterTokenSecret() {
        return twitterTokenSecret;
    }

    /**
     * @param twitterTokenSecret
     *            セットする twitterTokenSecret
     */
    public void setTwitterTokenSecret(String twitterTokenSecret) {
        this.twitterTokenSecret = twitterTokenSecret;
    }

    /**
     * @return tweet
     */
    public String getTweet() {
        return tweet;
    }

    /**
     * @param tweet
     *            セットする tweet
     */
    public void setTweet(String tweet) {
        this.tweet = tweet;
    }

    /**
     * @return schedules
     */
    public List<SchedulePostType> getSchedules() {
        return schedules;
    }

    /**
     * @param schedules
     *            セットする schedules
     */
    public void setSchedules(List<SchedulePostType> schedules) {
        this.schedules = schedules;
    }

    /**
     * @return startYear
     */
    public int getStartYear() {
        return startYear;
    }

    /**
     * @param startYear
     *            セットする startYear
     */
    public void setStartYear(int startYear) {
        this.startYear = startYear;
    }

    /**
     * @return startMonth
     */
    public int getStartMonth() {
        return startMonth;
    }

    /**
     * @param startMonth
     *            セットする startMonth
     */
    public void setStartMonth(int startMonth) {
        this.startMonth = startMonth;
    }

    /**
     * @return startDay
     */
    public int getStartDay() {
        return startDay;
    }

    /**
     * @param startDay
     *            セットする startDay
     */
    public void setStartDay(int startDay) {
        this.startDay = startDay;
    }

    /**
     * @return startHour
     */
    public int getStartHour() {
        return startHour;
    }

    /**
     * @param startHour
     *            セットする startHour
     */
    public void setStartHour(int startHour) {
        this.startHour = startHour;
    }

    /**
     * @return startMinute
     */
    public int getStartMinute() {
        return startMinute;
    }

    /**
     * @param startMinute
     *            セットする startMinute
     */
    public void setStartMinute(int startMinute) {
        this.startMinute = startMinute;
    }

    /**
     * @return endYear
     */
    public int getEndYear() {
        return endYear;
    }

    /**
     * @param endYear
     *            セットする endYear
     */
    public void setEndYear(int endYear) {
        this.endYear = endYear;
    }

    /**
     * @return endMonth
     */
    public int getEndMonth() {
        return endMonth;
    }

    /**
     * @param endMonth
     *            セットする endMonth
     */
    public void setEndMonth(int endMonth) {
        this.endMonth = endMonth;
    }

    /**
     * @return endDay
     */
    public int getEndDay() {
        return endDay;
    }

    /**
     * @param endDay
     *            セットする endDay
     */
    public void setEndDay(int endDay) {
        this.endDay = endDay;
    }

    /**
     * @return endHour
     */
    public int getEndHour() {
        return endHour;
    }

    /**
     * @param endHour
     *            セットする endHour
     */
    public void setEndHour(int endHour) {
        this.endHour = endHour;
    }

    /**
     * @return endMinute
     */
    public int getEndMinute() {
        return endMinute;
    }

    /**
     * @param endMinute
     *            セットする endMinute
     */
    public void setEndMinute(int endMinute) {
        this.endMinute = endMinute;
    }

    /**
     * 表示用Tweet取得
     *
     * @return
     */
    public String getViewTweet() {
        return this.tweet.substring(0, 8);
    }

    /**
     * 表示用スケジュール取得
     *
     * @return
     */
    public String getViewSchedules() {
        List<String> scheduleViewStringList = new ArrayList<String>();
        for (SchedulePostType type : this.schedules) {
            scheduleViewStringList.add(type.getName());
        }
        return StringUtil.join(scheduleViewStringList);
    }

    public boolean isScheduleMatch(Calendar cal) {
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);

        if (!isStartDateAway(year, month, day, hour, minute)) {
            return false;
        }
        SchedulePostType dayWeek =
            SchedulePostType.parse(cal.get(Calendar.DAY_OF_WEEK));
        if (!isWeekMatch(dayWeek)) {
            return false;
        }

        return true;
    }

    public boolean isStartDateAway(Calendar cal) {
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);

        return isStartDateAway(year, month, day, hour, minute);
    }

    public boolean isStartDateAway(int year, int month, int day, int hour,
            int minute) {
        return (this.startYear >= year
            && this.startMonth >= month
            && this.startDay >= day
            && this.startHour >= hour && this.startMinute >= minute);
    }

    public boolean isWeekMatch(SchedulePostType week) {
        for (SchedulePostType schedule : this.schedules) {
            if (schedule.equals(week)) {
                return true;
            }
        }
        return false;
    }

    /**
     * インスタンス生成
     */
    public static AutoPostVar getInstance(Object obj) {
        AutoPostVar instance = new AutoPostVar();
        BeanUtil.copy(obj, instance);
        if (StringUtil.isEmpty(instance.getId())) {
            String randomIdSeed =
                DateUtil.getNow().toString()
                    + String.valueOf(RandomUtil.nextLong());
            instance.setId(SecurityUtil.encrypt(randomIdSeed));
        }
        if (instance.getSchedules() == null) {
            instance.setSchedules(new ArrayList<SchedulePostType>());
        }
        return instance;
    }
}
